<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员管理类
 * @package  module/base/customer
 * @author   create by allen.qiang
 * @version 1.0
 */

class custComment {

    public function searchCustCommentDetail( &$arg_array ){

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //SQL字段类型字符串
        $params_type = '';

        //SQL字段值数组
        $params_array = array();

        //用户名
        if (isset($arg_array['comment_id']) && is_numeric($arg_array['comment_id'])) {
            $sql .= ' AND comment_id = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['comment_id']);
        }

        //用户名
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['pcustID']);
        }

         //客户唯一标识
        if (isset($arg_array['session_id']) && !empty($arg_array['session_id'])) {
            $sql .= ' AND session_id = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['session_id']);
        }

        //是否会员
        if (isset($arg_array['is_login']) && is_numeric($arg_array['is_login'])) {
            $sql .= ' AND is_login = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['is_login'];
        }

        //反馈者IP
        if (isset($arg_array['ip']) && !empty($arg_array['ip'])) {
            $sql .= ' AND ip = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['ip']);
        }

        //反馈者IP
        if (isset($arg_array['createTime']) && !empty($arg_array['createTime'])) {
            $sql .= ' AND createTime BETWEEN ? AND ? ';
            $params_type .= 'ss';
            $params_array[] =$arg_array['createTime'] . ' 00:00:00';
            $params_array[] =$arg_array['createTime'] . ' 23:59:59';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_comment ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询分类信息
            $search_sql = 'SELECT * FROM cust_comment ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;


            foreach ($temp_item_array as &$item) {

                //用户信息数组
                $item_array[]  = array(
                    'uuid'	    => 	$item->uuid,
                    'timeStamp'	=>	$item->timeStamp,
                    'creator' 	=> 	$item->creator,
                    'createTime' => $item->createTime,
                    'comment_id' => $item->comment_id,
                    'pcustID'	=> 	$item->pcustID,
                    'session_id'	=> 	$item->session_id,
                    'is_login'	=>	$item->is_login,
                    'option_content'		=> 	$item->option_content,
                    'auto_content'=>	$item->auto_content,
                    'ip'	=>	$item->ip
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    public function saveCustComment( &$arg_array ){

        global $G_SHOP;

        if(!isset($arg_array['session_id']) && empty($arg_array['session_id']) || !isset($arg_array['ip']) && empty($arg_array['ip'])){
            return array('message'=>'参数错误','code'=> 400);
        }

        if(!isset($arg_array['option_content']) || empty($arg_array['option_content'])){
            return array('message'=>'请选择您要提交的反馈选项','code'=> 400);
        }

        try{

            //查询客户最后反馈的数据
            $sql = 'SELECT * FROM cust_comment WHERE session_id =  '. '\'' . $arg_array['session_id'] . '\''  .' ORDER BY createTime DESC LIMIT 0 , 1 ';
            $result_array = $G_SHOP->DBCA->stmt_query($sql);

            //判断用户提交的时间是否在5分钟之内
            $new = time();
            if($result_array){
                if(($new - strtotime($result_array[0]->createTime)) <= 300){
                    return array('message'=>'您的反馈已提交，请不要频繁提交','code'=> 400);
                }
            }

            //把选项数组转换为字符串
            $option_content = implode(',' , $arg_array['option_content']);

            $cust_comment = new entity('cust_comment');

            //保存会员消息信息
            $cust_comment->comment_id = generate_seqno('seqno_comment');
            $cust_comment->creator = $arg_array['creator'] == "" ? 'system' : $arg_array['creator'];
            $cust_comment->pcustID = $G_SHOP->currentPCustID;
            $cust_comment->session_id = $arg_array['session_id'];
            $cust_comment->is_login =  !$G_SHOP->currentPCustID ? 0 : 1;
            $cust_comment->option_content = $option_content;
            $cust_comment->auto_content = $arg_array['auto_content'];
            $cust_comment->ip = $arg_array['ip'];

            $G_SHOP->DBCA->save( $cust_comment );

            return array( 'code' => 200, 'message' => '保存成功' );

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    public function getCustByOpenID( $openId )
    {
        global $G_SHOP;
        $sql = "SELECT * FROM cust_master WHERE wx_openid = '".$openId."'";
        $temp_array = $G_SHOP->DBCA->stmt_query($sql);
        if ($temp_array) {
            return $temp_array[0];
        }
        return null;
    }


}